﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using Models;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AnesSystem.ViewModels.WorkScore
{
    class WorkScoreStatisticsCtrlViewMode : BaseViewModel<WorkScoreStatisticsCtrlViewMode>
    {

        private ObservableCollection<BaseCountModel> _disDateStrModelList;
        private ObservableCollection<WorkScoreModel> _disWorkScoreModelList;
        private DateTime currentTime = DateTime.Now;
        public static Dictionary<string, List<PatientLinkModel>> chuFangTypeMap = null;
        private DateTime _startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        private DateTime _endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

        #region Variables

        #endregion

        #region Constructor
        public WorkScoreStatisticsCtrlViewMode()
        {
            DisDateStrModelList = new ObservableCollection<BaseCountModel>();
            DisWorkScoreModelList = new ObservableCollection<WorkScoreModel>();
        }

        public ObservableCollection<BaseCountModel> DisDateStrModelList
        {
            get
            {
                return _disDateStrModelList;
            }

            set
            {
                _disDateStrModelList = value;
                RaisePropertyChanged("SelDateModel");
            }
        }

        public ObservableCollection<WorkScoreModel> DisWorkScoreModelList
        {
            get
            {
                return _disWorkScoreModelList;
            }

            set
            {
                _disWorkScoreModelList = value;
                RaisePropertyChanged("DisWorkScoreModelList");
            }
        }

        #endregion

        #region Properties

        public DateTime StartTime
        {
            get
            {
                return _startTime;
            }

            set
            {
                _startTime = value;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return _endTime;
            }

            set
            {
                _endTime = value;
            }
        }

        public DateTime CurrentTime
        {
            get
            {
                return currentTime;
            }

            set
            {
                currentTime = value;
                RaisePropertyChanged("CurrentTime");
            }
        }

        public void Seach()
        {

        }

        public void WorkScoreDcexcel()
        {
            ExportToExcel excel = new ExportToExcel();
            Dictionary<DateTime,List<WorkScoreModel>> allWoekScoreList = new Dictionary<DateTime,List<WorkScoreModel>>();
            foreach (var baseModel in DisDateStrModelList)
            {
                DateTime dateTime = Convert.ToDateTime(baseModel.Name);
                List<WorkScoreModel> workList = WorkScoreService.Instance.findWorkScoreListByDateAndDoceName(dateTime, dateTime.AddDays(1));
                allWoekScoreList.Add(dateTime,workList);
            }
            excel.exportWorkScoreExcel(allWoekScoreList);
        }

        public void AddWorkScore()
        {
            WorkScoreModel work = new WorkScoreModel();
            work.DateDD = CurrentTime;
            work.PropertyChanged += initBind;
            work.Ysxm = "";
            WorkScoreService.Instance.AddUpdate(work);
            DisWorkScoreModelList.Add(work);
        }

        private void initBind(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            WorkScoreModel workScore = sender as WorkScoreModel;
            WorkScoreService.Instance.AddUpdate(workScore);
        }

        public void SelectDat(DateTime startDateTime, DateTime endDateTime)
        {
            DisDateStrModelList.Clear();
            int asds = ((int)(endDateTime - startDateTime).TotalSeconds / 3600 / 24) + 1;
            startDateTime.AddDays(-1);
            for (int i = 0; i < asds; i++)
            {
                BaseCountModel dateStr = new BaseCountModel();
                dateStr.Name = startDateTime.AddDays(i).ToShortDateString();
                DisDateStrModelList.Add(dateStr);

            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDrugsModelService.Instance.Update(sender as ConfigDrugsModel);
        }
        #endregion

        #region Public/Protected Methods
        public void UpdateDisWorkScoreModelList(BaseCountModel SelBaseCountModel)
        {
            DisWorkScoreModelList.Clear();
            if (SelBaseCountModel != null && SelBaseCountModel.Name != null)
            {
                CurrentTime = Convert.ToDateTime(SelBaseCountModel.Name);
                List<WorkScoreModel> workList = WorkScoreService.Instance.findWorkScoreListByDateAndDoceName(CurrentTime, CurrentTime.AddDays(1));
                foreach (var work in workList)
                {
                    work.PropertyChanged += initBind;
                    DisWorkScoreModelList.Add(work);
                }
            }
        }

        #endregion

        #region EventHandlers

        #endregion
    }


}
